import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaofanshu_flutter/mapper/attention_message_mapper.dart';

class AttentionMessageController extends GetxController {
  var attentionMessageList = [].obs;
  ScrollController scrollController = ScrollController();
  bool isLoading = false;

  @override
  void onInit() {
    super.onInit();
    onRefresh();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        loadMore();
      }
    });
  }

  Future<void> onRefresh() async {
    attentionMessageList.clear();
    var list = await AttentionMessageMapper.queryAll();
    attentionMessageList.addAll(list);
  }

  Future<void> loadMore() async {
    if (isLoading) return;
    isLoading = true;
    // 这里可以实现分页加载逻辑
    isLoading = false;
  }

  void deleteMessage(int id) async {
    await AttentionMessageMapper.delete(id);
    onRefresh();
  }

  void clearAll() async {
    await AttentionMessageMapper.clearAll();
    onRefresh();
  }
}